<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/";
    request.setAttribute("basePath", basePath);
%>
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${basePath}css/font.css">
    <link rel="stylesheet" href="${basePath}css/xadmin.css">
    <script type="text/javascript" src="${basePath}js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="${basePath}lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${basePath}js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="${basePath}css/xcConfirm.css" type="text/css">
    <script src="${basePath}lib/My97DatePicker/WdatePicker.js"></script>
    <script src="${basePath}js/xcConfirm.js"></script>
</head>
<script>
    $(function () {
        try {
            var index = parent.layer.getFrameIndex(window.name);
            window.parent.location.reload();
            parent.layer.close(index);
        } catch (e) {

        }
    });
</script>
<body>
<div class="x-body connect_wrap">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so layui-form-pane" action="${basePath}MemberCoupons/giftCouponSerach" method="get">
            <input class="layui-input" placeholder="用户电话" name="telephone" value="${page.conMap.telephone}"
                   id="telephone">
            <input class="layui-input" placeholder="订单支付时间-开始" name="orderBeginTime" value="${page.conMap.orderBeginTime}"
                   onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" id="orderBeginTime">
            <input class="layui-input" placeholder="订单支付时间-结束" name="orderEndTime" value="${page.conMap.orderEndTime}"
                   onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" id="orderEndTime">
            <input class="layui-input" placeholder="订单支付金额-开始" name="orderStartAmount" id="orderStartAmount"
                   value="${page.conMap.orderStartAmount}">
            <input class="layui-input" placeholder="订单支付金额-结束" name="orderEndAmount" id="orderEndAmount"
                   value="${page.conMap.orderEndAmount}">
            <div class="layui-input-inline">
                <select name="ddlCoupon" id="ddlCoupon">
                    <option value="">请选择优惠券</option>
                    <c:forEach items="${list}" var="p">
                        <option
                                <c:if test="${page.conMap.ddlCoupon eq p.id}">selected="selected"</c:if>
                                value="${p.id}">${p.id} [${p.title }]
                        </option>
                    </c:forEach>
                </select>
            </div>
            <span style="color:red;">（请您在执行一键批量赠送前，先填写查询条件，点击查询按钮后，确定筛选学员后再进行赠送！！！）</span>
            <button class="layui-btn" type="submit"><i class="layui-icon">&#xe615;</i></button>
        </form>
    </div>
    <xblock>
        <button class="layui-btn" lay-filter="sreach" onclick="Gift()">
            <i class="layui-icon"></i>赠送
        </button>
        <button class="layui-btn" lay-filter="sreach"
                onclick="if(!confirm('您确定要赠送筛选出来的人员优惠券吗？请您核对好筛选条件!')){return false;}batchGift(1)">
            <i class="layui-icon"></i>全部赠送
        </button>
        <button class="layui-btn" lay-filter="sreach"
                onclick="if(!confirm('您确定要赠送筛选出来的人员优惠券吗？请您核对好筛选条件!')){return false;}batchGift(2)">
            <i class="layui-icon"></i>已购用户赠送
        </button>
        <span class="x-right" style="line-height:40px">共有数据：${requestScope.page.totalCount} 条</span>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <td><input id="allCkb" type="checkbox"/></td>
            <td>序号</td>
            <td>用户id</td>
            <td>用户昵称</td>
            <td>用户电话</td>
            <td>用户来源</td>
            <td>用户状态</td>
            <td>是否黑名单</td>
            <td>支付总金额</td>
        </tr>
        </thead>
        <tbody>
        <c:if test="${not empty page.results}">
            <c:forEach items="${requestScope.page.results}" var="result" varStatus="vs">
                <tr>
                    <td><input type='checkbox' name='ckb' value="${result.userId }"/></td>
                    <td>${(page.actulPageNo - 1) * 15 + vs.count}</td>
                    <td>${result.userId}</td>
                    <td>${result.userName}</td>
                    <td>${result.telephone}</td>
                    <td>${result.userSource}</td>
                    <td>
                        <c:if test="${result.status eq '1'}">启用</c:if>
                        <c:if test="${result.status eq '0'}">停用</c:if>
                    </td>
                    <td>
                        <c:if test="${result.blackFlag eq '1'}">是</c:if>
                        <c:if test="${result.blackFlag eq '0'}">否</c:if>
                    </td>
                    <td>${result.orderAmount }</td>
                </tr>
            </c:forEach>
        </c:if>
        </tbody>
        <input type="hidden" id="totalCounts" value="${page.totalCount}"/>
    </table>
    <div class="page">
        <c:if test="${requestScope.page.totalPageNo > 1 }">
            <div>
                <a class="num" href='${basePath}${page.url}?pageNo=1${page.params}'>首页</a>
                <c:if test="${page.actulPageNo > 1 }">
                    <a class="prev"
                       href='${basePath}${page.url}?pageNo=${page.actulPageNo - 1}${page.params}'>上一页</a>
                </c:if>
                <c:forEach var="pno" items="${page.showNums }">
                    <c:choose>
                        <c:when test="${pno == page.actulPageNo}">
                            <span class="current"> ${pno }</span>
                        </c:when>
                        <c:otherwise>
                            <c:if test="${not empty pno}">
                                <a class="num"
                                   href='${basePath}${page.url}?pageNo=${pno  }${page.params}'>${pno }</a>
                            </c:if>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>
                <c:if test="${page.actulPageNo  < page.totalPageNo }">
                    <a class="next" href='${basePath}${page.url}?pageNo=${page.actulPageNo + 1}${page.params}'>下一页</a>
                </c:if>
                <a class="num" href='${basePath}${page.url}?pageNo=${page.totalPageNo }${page.params}'>尾页</a>
            </div>
        </c:if>
    </div>
</div>
<script>
    layui.use(['form'], function () {
        form = layui.form;

    });

    $("#allCkb").click(function () {
        if (this.checked) {
            $(".connect_wrap :checkbox").prop("checked", true);
        } else {
            $(".connect_wrap :checkbox").prop("checked", false);
        }
    });

    function Gift() {
        //批量赠送
        var ids = [];
        $("input[name=ckb]:checked").each(function () {
            ids.push($(this).val());
        });
        if (ids.length <= 0) {
            var txt = "请选择要给分配优惠券的人员！";
            window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error);
            return false;
        }
        ;
        var ddlvlue = $("#ddlCoupon").val();
        if (ddlvlue == '') {
            var txt = "要选择优惠券哦！";
            window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error);
            return false;
        }
        ;
        var msg = '您的筛选条件为：\n';
        if ($("#telephone").val().trim().length > 0) {
            msg += "用户电话：" + $("#telephone").val().trim() + "\n";
        }
        ;
        if ($("#orderBeginTime").val().trim().length > 0) {
            msg += "订单支付时间-开始：" + $("#orderBeginTime").val().trim() + "\n";
        }
        ;
        if ($("#orderEndTime").val().trim().length > 0) {
            msg += "订单支付时间-结束：" + $("#orderEndTime").val().trim() + "\n";
        }
        ;
        if ($("#orderStartAmount").val().trim().length > 0) {
            msg += "订单支付金额-开始：" + $("#orderStartAmount").val().trim() + "\n";
        }
        ;
        if ($("#orderEndAmount").val().trim().length > 0) {
            msg += "订单支付金额-结束：" + $("#orderEndAmount").val().trim() + "\n";
        }
        ;
        var yhqlx = $("#ddlCoupon").find("option:selected").text();
        msg += "优惠券类型：" + yhqlx + "\n";
        msg += "即将送出：" + ids.length + "张优惠券\n";
        var confrim = confirm(msg);
        if (confrim) {
            $.ajax({
                async: false,
                type: "post",
                url: '${basePath}MemberCoupons/gitf',
                data: {"id": ddlvlue, "userId": ids},
                traditional: true,  //传递数组到后台，注意这个参数是必须的
                dataType: 'text',
                success: function (data) {
                    if (data == "1") {
                        var txt = "赠送优惠券成功！成功赠送" + ids.length + "张优惠券";
                        var option = {
                            title: "成功",
                            onOk: function () {
                                window.location.href = '${basePath}MemberCoupons/giftCoupon';
                            }
                        }
                        window.wxc.xcConfirm(txt, "custom", option);
                    } else {
                        var txt = "赠送优惠券失败！";
                        var option = {
                            title: "失败",
                            onOk: function () {
                                window.location.href = '${basePath}MemberCoupons/giftCoupon';
                            }
                        }
                        window.wxc.xcConfirm(txt, "custom", option);
                    }
                }
            });
        } else {
            alert("请重新添加查询条件！");
            window.location.href = '${basePath}MemberCoupons/giftCoupon';
            return false;
        }
    };

    function batchGift(flag) {
        var telephone = $("#telephone").val();
        var orderBeginTime = $("#orderBeginTime").val();
        var orderEndTime = $("#orderEndTime").val();
        var orderStartAmount = $("#orderStartAmount").val();
        var orderEndAmount = $("#orderEndAmount").val();
        var couponId = $("#ddlCoupon").val();
        if (couponId == '') {
            var txt = "要选择优惠券哦！";
            window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error);
            return false;
        }
        var totalCounts = $("#totalCounts").val() || '';
        if (totalCounts == '') {
            alert("请您先点击查询按钮，根据查询条件进行优惠券赠送！");
            window.location.href = '${basePath}MemberCoupons/giftCoupon';
            return false;
        }
        var msg = '您的筛选条件为：\n';
        if ($("#telephone").val().trim().length > 0) {
            msg += "用户电话：" + $("#telephone").val().trim() + "\n";
        }
        ;
        if ($("#orderBeginTime").val().trim().length > 0) {
            msg += "订单支付时间-开始：" + $("#orderBeginTime").val().trim() + "\n";
        }
        ;
        if ($("#orderEndTime").val().trim().length > 0) {
            msg += "订单支付时间-结束：" + $("#orderEndTime").val().trim() + "\n";
        }
        ;
        if ($("#orderStartAmount").val().trim().length > 0) {
            msg += "订单支付金额-开始：" + $("#orderStartAmount").val().trim() + "\n";
        }
        ;
        if ($("#orderEndAmount").val().trim().length > 0) {
            msg += "订单支付金额-结束：" + $("#orderEndAmount").val().trim() + "\n";
        }
        ;
        var yhqlx = $("#ddlCoupon").find("option:selected").text();
        msg += "优惠券类型：" + yhqlx + "\n";
        msg += "即将送出：" + totalCounts + "张优惠券\n";
        var confrim = confirm(msg);
        if (confrim) {
            $.ajax({
                async: false,
                type: "get",
                url: '${basePath}MemberCoupons/batchGift',
                data: {
                    "couponId": couponId,
                    "telephone": telephone,
                    "orderBeginTime": orderBeginTime,
                    "orderEndTime": orderEndTime,
                    "orderStartAmount": orderStartAmount,
                    "orderEndAmount": orderEndAmount,
                    "flag": flag
                },
                traditional: true,  //传递数组到后台，注意这个参数是必须的
                dataType: 'text',
                beforeSend: function () {
                    $("#yjplzs").attr("disabled", true);
                },
                success: function (data) {
                    if (data != "0") {
                        var txt = "赠送优惠券成功！成功赠送" + data + "张优惠券";
                        var option = {
                            title: "成功",
                            onOk: function () {
                                window.location.href = '${basePath}MemberCoupons/giftCoupon';
                            }
                        }
                        window.wxc.xcConfirm(txt, "custom", option);
                    } else {
                        var txt = "赠送优惠券失败！";
                        var option = {
                            title: "失败",
                            onOk: function () {
                                window.location.href = '${basePath}MemberCoupons/giftCoupon';
                            }
                        }
                        window.wxc.xcConfirm(txt, "custom", option);
                    }
                }
            });
        } else {
            alert("请重新添加查询条件！");
            window.location.href = '${basePath}MemberCoupons/giftCoupon';
            return false;
        }
    };
</script>
</body>

</html>